package com.dhecp.project.hr.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.hr.domain.ProductSetup;
import com.dhecp.project.hr.mapper.ProductSetupMapper;
import com.dhecp.project.hr.service.ProductSetupService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 亚马逊地址设置
 * @author: fxz
 * @create: 2022-10-09 14:36
 */
@Service
public class ProductSetupServiceImpl extends BaseModuleServiceImpl implements ProductSetupService {

    @Autowired
    private ProductSetupMapper productSetupMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    
    /**
     * 查询商品库
     * @param params
     * @return List - ProductCarefullyChosen
     * @throws Exception
     * @author DH-Xxj
     */
    @Override
    public List<ProductSetup> queryDataList(Map<String, Object> params) throws Exception{
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句

        PageUtils.startPage();

        return productSetupMapper.select(params);
    }
    
	@Override
	public ProductSetup findByCode(String code) {
		
		return productSetupMapper.findByCode(code);
	}

	@Override
	public void insert(ProductSetup entity) {
		productSetupMapper.insert(entity);
	}

	@Override
	public AjaxResult editSetting(ProductSetup entity,
    		String sizePackage, String weightPackage, String advantage) {
		
		JSONObject infoBaseJO = entity.getInfoBaseJO();
		infoBaseJO.put("sizePackage", sizePackage);
		infoBaseJO.put("weightPackage", weightPackage);
		infoBaseJO.put("advantage", advantage);
		entity.setInfoBase(infoBaseJO.toJSONString());
		
		productSetupMapper.update(entity);
		return AjaxResult.success();
	}

	/**
	 * 导入产品信息至产品信息过渡表
	 * @param productBasisItems 必要参数：基础商品库对应实体类（ProductBasis）的json格式字符串
	 * @return MessageModel
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult importBatch(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		
		List<ProductSetup> list = JSON.parseArray(params.get("rows").toString(), ProductSetup.class);
		if (list == null || list.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}

		for (ProductSetup entity : list) {
			if (entity.getProductID() == null || entity.getProductID().length() == 0) {
				return AjaxResult.error("【商品编码】不能为空");
			}
		}
		
		System.out.println(JSON.toJSONString(list));

		for (ProductSetup entity : list) {
			
			productSetupMapper.updateByCode(entity);
		}
		return AjaxResult.success("操作成功");
	}
}